﻿<%@ WebHandler Language="C#" Class="autocomplete" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Collections.Generic;

public class autocomplete : AjaxHandlerBase {

    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string companyid = "";
        if (!string.IsNullOrEmpty(context.Request.QueryString["companyid"]))
        {
            companyid = context.Request.QueryString["companyid"];
        }
        StringBuilder suggestions = new StringBuilder();
        StringBuilder data = new StringBuilder();
        StringBuilder str = new StringBuilder();
        str.Append("{query:'" + context.Request.QueryString["query"] + "',");
        suggestions.Append("suggestions:[");
        data.Append("data:[");

        //自动填充楼盘名
        if (companyid.Length < 1)
        {
            System.Collections.Generic.List<Chenxin.Lib.House.Entity.Community_BaseInfo> cbInfoList = getAutoCompleteData(context.Request.QueryString["query"]);
            foreach (Chenxin.Lib.House.Entity.Community_BaseInfo item in cbInfoList)
            {
                suggestions.Append("'" + item.CommunityName + "',");
                data.Append("\"{");
                data.Append(" CommunityId : '" + item.CommunityId + "',");
                data.Append(" Address : '" + item.Address + "',");
                data.Append(" Region : '" + item.Region + "',");
                data.Append(" Block : '" + item.Block + "',");
                data.Append(" CommunityUse : '" + item.CommunityUse + "',");
                data.Append(" BuildingType : '" + item.BuildingType + "',");
                data.Append(" CompleteYear: '" + item.CompleteYear + "'");
                data.Append(" }\" ,");
            }
        }
        //自动填充部门名称(组织结构—人员设定中用到)
        else
        {
            if (companyid == "0")
                companyid = Guid.Empty.ToString();
            string sql = string.Format("DepartName like '{0}%' and CompanyId='{1}' and (DepartProperty = 500 or DepartProperty=600)", context.Request.QueryString["query"], companyid);
            List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> deplist = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetListArray(sql);
            foreach (Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo dep in deplist)
            {
                suggestions.Append("'" + dep.DepartName + "',");
                data.Append("'" + dep.DepartId.ToString() + "',");
            }
        }

        if(suggestions.Length>13)
            suggestions.Remove((suggestions.Length-1), 1);
        if(data.Length>7)
            data.Remove((data.Length - 1), 1);
        suggestions.Append("],");
        data.Append("]");
        str.Append(suggestions);
        str.Append(data);
        str.Append("}");
        context.Response.Write(str);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    //*******************************************
    //功能概要       ：自动填充
    //参数説明       ：uName 名称
    //返回值説明     ：DataSet
    //功能詳細       ：根据输入显示相似楼盘名称
    //作成者         ：chen.yugong 
    //作成日         ：2010-02-22 
    //修改者         ：
    //修改日         ：
    //*******************************************
    private System.Collections.Generic.List<Chenxin.Lib.House.Entity.Community_BaseInfo> getAutoCompleteData(String communityName)
    {
        string strWhere = string.Format("(CommunityName like '{0}') OR (Py like '{1}')", "%" + communityName + "%", "%" + communityName + "%");
        return Chenxin.Lib.House.DAL.Community_BaseInfo.GetListArray(strWhere);
    }    
}